﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Text;
using System.Windows.Forms;
using eMisc;

namespace eLib.ImportWizard
{
    public delegate void XlsSelectChange();

    public partial class XlsSelectControl : UserControl
    {
        public XlsSelectControl()
        {
            InitializeComponent();
        }

        public XlsSelectControl(string filename, string sheetname)
        {
            InitializeComponent();
            eFile.Tag = "E"; FileName = filename; eFile.Tag = null;
            cbSheet.Tag = "E"; cbSheet.Text = sheetname;cbSheet.Tag = null;
        }

        public XlsSelectChange OnChange;

        bool changed = false;

        public bool Changed { get { return changed; } }

        public void ResetChanges()
        {
            changed = false;
        }

        private void doChange()
        {
            if (OnChange != null)
                OnChange();
            changed = true;
        }

        public string FileName
        {
            get { return eFile.Text; }
            set
            {
                if (string.Compare(eFile.Text, value, true) != 0)
                {
                    string old_sh = cbSheet.Text;
                    cbSheet.Tag = "E";
                    string err;
                    WorkSheetUtils.GetSheetNames(value, cbSheet.Items, out err);
                    for (int i = 0; i < cbSheet.Items.Count; i++)
                        if (string.Compare(cbSheet.Items[i].ToString(), old_sh, true) == 0)
                        {
                            cbSheet.SelectedIndex = i;
                            break;
                        }
                    eFile.Text = value;
                    cbSheet.Tag = null;
                }
            }
        }

        public string SheetName
        {
            get { return cbSheet.Text; }
            set
            {
                if (string.Compare(cbSheet.Text, value, true) != 0)
                    cbSheet.Text = value;
            }
        }

        private void eFile_TextChanged(object sender, EventArgs e)
        {
            Control ct = (Control)sender;
            if (ct.Tag != null && ct.Tag.ToString() != "E") doChange(); 
        }

        private void btnFile_Click(object sender, EventArgs e)
        {
            if (openFileDialog.ShowDialog() == DialogResult.OK)
                FileName = openFileDialog.FileName;
        }
    }
}
